package cn.iocoder.yudao.module.wms.controller.admin.checkvouch;

import cn.iocoder.yudao.module.wms.controller.admin.rdrecord10.vo.Rdrecord10RespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord10.Rdrecord10DO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.checkvouch.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.checkvouch.CheckvouchDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.checkvouch.CheckvouchsDO;
import cn.iocoder.yudao.module.wms.service.checkvouch.CheckvouchService;

@Tag(name = "管理后台 - 盘点")
@RestController
@RequestMapping("/wms/checkvouch")
@Validated
public class CheckvouchController {

    @Resource
    private CheckvouchService checkvouchService;

    @PostMapping("/create")
    @Operation(summary = "创建盘点")
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:create')")
    public CommonResult<Integer> createCheckvouch(@Valid @RequestBody CheckvouchSaveReqVO createReqVO) {
        return success(checkvouchService.createCheckvouch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新盘点")
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:update')")
    public CommonResult<Boolean> updateCheckvouch(@Valid @RequestBody CheckvouchSaveReqVO updateReqVO) {
        checkvouchService.updateCheckvouch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除盘点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:delete')")
    public CommonResult<Boolean> deleteCheckvouch(@RequestParam("id") Integer id) {
        checkvouchService.deleteCheckvouch(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得盘点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:query')")
    public CommonResult<CheckvouchRespVO> getCheckvouch(@RequestParam("id") String id) {


        try {
            CheckvouchDO checkvouch = checkvouchService.getCheckvouch(Integer.valueOf(id));
            return success(BeanUtils.toBean(checkvouch, CheckvouchRespVO.class));
        }catch (Exception e)
        {
            CheckvouchDO  checkvouch=  checkvouchService.findByProcessInstanceId(id);
            return success(BeanUtils.toBean(checkvouch, CheckvouchRespVO.class));
        }
    }

    @GetMapping("/page")
    @Operation(summary = "获得盘点分页")
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:query')")
    public CommonResult<PageResult<CheckvouchRespVO>> getCheckvouchPage(@Valid CheckvouchPageReqVO pageReqVO) {
        PageResult<CheckvouchDO> pageResult = checkvouchService.getCheckvouchPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CheckvouchRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出盘点 Excel")
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCheckvouchExcel(@Valid CheckvouchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CheckvouchDO> list = checkvouchService.getCheckvouchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "盘点.xls", "数据", CheckvouchRespVO.class,
                        BeanUtils.toBean(list, CheckvouchRespVO.class));
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Integer id) {
        checkvouchService.process(id);
        return success(true);
    }

    // ==================== 子表（盘点子） ====================

    @GetMapping("/checkvouchs/list-by-pid")
    @Operation(summary = "获得盘点子列表")
    @Parameter(name = "pid", description = "主表ID")
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:query')")
    public CommonResult<List<CheckvouchsDO>> getCheckvouchsListByPid(@RequestParam("pid") Integer pid) {
        return success(checkvouchService.getCheckvouchsListByPid(pid));
    }

    @GetMapping("/checkvouchs/getCheckvouchs")
    @Operation(summary = "获得盘点单子表详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:checkvouch:query')")
    public CommonResult<CheckvouchsRespVO> getCheckvouchs(@RequestParam("id") Integer id) {
        return success( checkvouchService.findCheckvouchsById(id));
    }

}